<template>
  <div class="view-wrapper">
    <div class="head">
      <el-form :inline="true" :model="formInline" class="demo-form-inline"> 
        <el-form-item label="优惠券名称：">
          <el-input placeholder="请输入优惠券名称" v-model="formInline.couponName" class="input-with-select">
          </el-input>
        </el-form-item>
        <el-form-item label="优惠券类型：">
          <el-select v-model="formInline.couponType" placeholder="请选择">
            <el-option v-for="item in typeLs" :key="item.id" :label="item.value" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="活动状态：">
          <el-select v-model="formInline.activityStatus" placeholder="请选择">
            <el-option v-for="item in openLs" :key="item.id" :label="item.label" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发放渠道：">
          <el-select v-model="formInline.areaId" placeholder="请选择" style="margin-left: 10px;" @change="areaChange">
            <el-option label="平台领劵中心" value=""></el-option>
            <el-option v-for="(aitem, aindex) in areaList" :key="aindex" :label="aitem.name" :value="aitem.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="活动时间：">
          <el-date-picker v-model="date" type="daterange" value-format="yyyy-MM-dd" range-separator="至"
            start-placeholder="开始日期" end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button @click="reset">重置</el-button>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="listBox">
      <div class="listBox_head">
        <el-button type="primary" @click="getNew">添加优惠券活动</el-button>
        <el-button type="primary" @click="getUrl">复制领券中心链接</el-button>
      </div>
      <ex-table ref="table" :loading="loading" :table-column="tableColumn" :table-data="tableData"
        @pageSizeChange="handleSizeChange" @currentPageChange="handleCurrentChange">
        <template v-slot:operateTime="{ scope }">
          <div>{{scope.row.activityStartTime}}</div>
          <div>{{scope.row.activityEndTime}}</div>
        </template>
        <template v-slot:vouchersType="{ scope }"> 
           <dict-tag :options="typeLs" :value="scope.row.couponType" /> 
        </template>
        <template v-slot:operate="{ scope }">
          <el-button @click="edit(scope.row.id)" size="small" v-if="scope.row.platformType=='1'">编辑</el-button>
          <el-button @click="edit(scope.row.id)" size="small" v-if="scope.row.platformType!='1'">查看</el-button>
          <el-button type="primary" size="small" @click="getDetailUrl(scope.row.id)">复制链接</el-button>
        </template>
      </ex-table>
    </div>
  </div>
</template>
<script>
  // components
  import {
    adminDict,
    couponList,
    getUrl,
    getDetailUrl
  } from "@/api/couponManagement";
  import {
    kyyBannerConfigGovAreaList
} from '@/api/clientManagement/businessSetting'
  import handlePageMixin from "@/mixins/handlePageMixin";
  import ExportExcelButton from "@/components/ExportExcelButton/index";
  // import { values } from 'mock/article';
  export default {
    //   name: "",
    components: {
      ExportExcelButton,
    },
    mixins: [handlePageMixin],
    data() {
      return {
        changeIndex: 0,
        loading: false,
        areaList:[],//发放渠道列表
        formInline: {
          couponName: "", //优惠券名称
          couponType: "", //优惠券类型;(1 满减券 2 折扣券 3 礼品券) platform_coupon_type
          activityStatus: "", //活动状态 1未启用 2待开始 3进行中 4已结束\
          platformType:'',//发放渠道
          areaId:null,//区域id
        },
        openLs: [{
            id: '',
            label: "全部"
          },
          {
            id: '1',
            label: "未启用"
          },
          {
            id: '2',
            label: "待开始"
          },
          {
            id: '3',
            label: "进行中"
          },
          {
            id: '4',
            label: "已结束"
          },
        ],
        typeLs: [], //优惠券类型数组
        date: [],
        tableData: [],
        centerUrl: '领券中心链接', //复制领券中心链接
        tableColumn: [{
            prop: "platformName",
            label: "发放渠道"
          },
          {
            prop: "activityName",
            label: "活动名称",
            width: 150
          },
          {
            label: "活动时间",
            slot: true,
            slotName: "operateTime",
            width: "180",
          },
          {
            prop: "couponName",
            label: "优惠券名称",
            width: 130
          },
          // {
          //   prop: "couponTypeDict",
          //   label: "优惠券类型",
          //   width: 130
          // },
           {
          
            label: "优惠券类型",
            width: 130,
            slot: true,
            slotName: "vouchersType",
          },
          {
            prop: "validityTypeDict",
            label: "优惠券有效期",
            width: 140
          },
          {
            prop: "preferentialCondition",
            label: "优惠券条件",
            width: 130
          },
          {
            prop: "preferentialDiscount",
            label: "优惠力度"
          },
          {
            prop: "couponStock",
            label: "总库存"
          },
          {
            prop: "grantNum",
            label: "发放数量"
          },
          {
            prop: "writeOffNum",
            label: "核销数量"
          },
          {
            prop: "unUsedNum",
            label: "未使用数量",
            width: 130
          },
          {
            prop: "overdueNum",
            label: "已过期数量",
            width: 140
          },
          {
            prop: "activityStatusDict",
            label: "活动状态",
            width: 150
          },
          {
            label: "操作",
            slot: true,
            slotName: "operate",
            width: "180",
            fixed: "right",
          },
        ],
      };
    },
    created() {
      this.getTypeList();
      this.getKyyBannerConfigGovAreaList();
      this.getList();
    },
    computed: {},
    methods: {
      getTypeList() {
        //获取字典
        const params = {
          dictType: "platform_coupon_type",
        };
        adminDict(params).then((res) => {
          // var arr = [
          //   {
          //     id: "",
          //     value: "全部",
          //   },
          // ];
          // this.options2 = arr.concat(res.result[0].dictList);
          this.typeLs = res.result[0].dictList;
        });
      },
        // 切换地区
        areaChange(){
            if(this.formInline.areaId == ''){
                this.formInline.platformType = 1
            }else{
                this.formInline.platformType = 2
            }
        },
       // 获取区域选择列表
       getKyyBannerConfigGovAreaList() {
          kyyBannerConfigGovAreaList().then((res) => {
              this.areaList = res.result
          });
        },
      copyCenter(value) { //复制
        console.log(value)
        if(value==undefined||value==''){
          this.$message.error('复制失败,url地址不存在')
          return
        }
        //创建input标签
        var input = document.createElement('input')
        //将input的值设置为需要复制的内容
        input.value = value;
        //添加input标签
        document.body.appendChild(input)
        //选中input标签
        input.select()
        //执行复制
        document.execCommand('copy')
        //成功提示信息
        this.$message.success('复制成功')
        //移除input标签
        document.body.removeChild(input)
      },
      getUrl(){ //复制领券中心地址
        const params = {};
        getUrl(params).then((res) => {
          this.copyCenter(res.result)
        });
      },
      getDetailUrl(id){ //复制详情链接
        const params = {
          id: id
        };
        getDetailUrl(params).then((res) => {
          this.copyCenter(res.result)
        });
      },
      getNew() {
        //新增
        this.$router.push({
          path: "/couponManagement/coupon-add",
        });
      },
      edit(id) {
        //编辑
        this.$router.push({
          path: "/couponManagement/coupon-add?type=" + 'edit' + '&id=' + id,
        });
      },
      onSubmit() {
        //查询
        console.log(this.formInline);
        this.pageOptions.pageNum = 1;
        this.$refs.table.pageOptions.pageNum = 1;
        this.getList();
      },
      reset() {
        this.formInline = {};
        this.date = [];
        this.pageOptions.pageNum = 1;
        this.$refs.table.pageOptions.pageNum = 1;
        this.getList();
      },
      getList() {
        const params = {
          ...this.pageOptions,
          ...this.formInline,
          activityStartTime: this.date[0],
          activityEndTime: this.date[1],
        };
        this.loading = true;
        couponList(params)
          .then((res) => {
            const {
              total,
              records
            } = res.result;
            records.forEach((item) => {
              if (item.activityStatus == "1") {
                item.activityStatusDict = '未启用'
              } else if (item.activityStatus == "2") {
                item.activityStatusDict = "待开始";
              } else if (item.activityStatus == "3") {
                item.activityStatusDict = "进行中";
              } else if (item.activityStatus == "4") {
                item.activityStatusDict = "已结束";
              }

              if (item.validityType == '2') {
                item.validityTypeDict = item.afterReceivingDay + '天后生效' + item.effectiveDays + '天'
              } else if (item.validityType == '3') {
                item.validityTypeDict = item.validityStartTime + '至' + item.validityEndTime
              }

              item.preferentialCondition = '满' + item.preferentialCondition + '可用'

              if (item.couponType == '1') {
                item.preferentialDiscount = item.preferentialDiscount + '元' //优惠力度
              } else if (item.couponType == '2') {
                item.preferentialDiscount = item.preferentialDiscount + '折' //折扣力度
              }
            });
            this.$refs.table.pageOptions.total = total;
            this.tableData = records;
          })
          .finally(() => {
            this.loading = false;
          });
      },
      lookDetail(id) {
        this.$router.push({
          path: "/clientManagement/shopDetail",
          query: {
            id: id
          },
        });
      },
    },
  };

</script>
<style lang="scss">
  .el-card-title {
    font-size: 20px;
  }

</style>
<style lang="scss" scoped>
  .head {
    box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
    background-color: #fff;
    border-radius: 8px;
    padding: 20px 20px 0;
  }

  .el-form--inline .el-form-item {
    margin-right: 30px;
  }

  .listBox {
    box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
    background-color: #fff;
    border-radius: 8px;
    //   height: 120px;
    padding: 20px 20px;
    margin-top: 10px;
  }

  .table_list {
    display: flex;
    align-items: center;
    // border: 1px solid #DCDFE6;
    border-radius: 7px;
    width: 220px;
    margin-bottom: 10px;
  }

  .tableBox {
    width: calc(100% / 3);
    height: 36px;
    text-align: center;
    line-height: 36px;
    color: #303133;
    font-size: 14px;
    border: 1px solid #dcdfe6;
    border-right: 0;
  }

  .tableBox:first-child {
    border-radius: 7px 0px 0px 7px;
  }

  .tableBox:last-child {
    border-right: 1px solid #dcdfe6;
    border-radius: 0px 7px 7px 0px;
  }

  .spchoosetext {
    background-color: #fff4e6;
    color: #ff9300;
  }

  .listBox_head {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 20px;
  }

  .el-select .el-input {
    width: 130px;
  }

  .input-with-select .el-input-group__prepend {
    background-color: #fff;
  }

  .head ::v-deep.el-input-group__prepend {
    width: 110px !important;
  }

</style>
